package org.lq.his.controller;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.lq.his.pojo.DoctorRankClass;
import org.lq.his.pojo.Drug;
import org.lq.his.service.DoctorRankClassService;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/doctorRankClass")
public class DoctorRankClassController {
	@Resource
	private DoctorRankClassService doctorRankClassService;

	public void setDoctorRankClassService(DoctorRankClassService doctorRankClassService) {
		this.doctorRankClassService = doctorRankClassService;
	}
	
	@RequestMapping("/index")
	public String drugIndex()throws Exception{
		return "DoctorRankClass/DoctorRankClassIndex";
	}
	@RequestMapping(value="/doctorRankClassForm",method=RequestMethod.GET)
	public String drugAdd()throws Exception{
		return "DoctorRankClass/DoctorRankClassForm";
	}

	@RequestMapping("/list") 
	@ResponseBody
	public Map<String, Object> list(@RequestParam(defaultValue="1")Integer  page,@RequestParam(defaultValue="5")Integer  rows,@RequestParam(defaultValue="id") String sort,@RequestParam(defaultValue="asc") String order, DoctorRankClass condition) throws Exception{
		
		Map<String, Object> map = new HashMap<>();
		int start = (page - 1)*rows;
		List<DoctorRankClass> list = doctorRankClassService.getListByCondition(start, rows, condition, sort,order);
		int total = doctorRankClassService.getCountByCondition(condition);
		map.put("total", total);
		map.put("rows", list);
		
		return map;
	}
	@RequestMapping(value="/add",method=RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> add(DoctorRankClass doctorRankClass)throws Exception{
		Map<String, Object> map = new HashMap<>();
		doctorRankClassService.add(doctorRankClass);
		map.put("result", true);
		return map;
	}
	@RequestMapping(value="/edit",method=RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> edit(DoctorRankClass doctorRankClass)throws Exception{
		Map<String, Object> map = new HashMap<>();
		doctorRankClassService.update(doctorRankClass);
		
		map.put("result", true);
		
		return map;
	}
	@RequestMapping("/view")
	@ResponseBody
	public DoctorRankClass view(Integer id)throws Exception{
		return doctorRankClassService.getById(id);
	}
	
	@RequestMapping("/leave")
	@ResponseBody
	public Map<String, Object> leave(Integer[] ids)throws Exception{
		doctorRankClassService.updateLeave(ids);
		Map<String, Object> map= new HashMap<>();
		map.put("result", true);
		return map;
	}
	@RequestMapping("/deletedoctorRankClass")
	@ResponseBody
	public Map<String, Object> delete(Integer[] ids)throws Exception{
		doctorRankClassService.deleteDoctorRankClass(ids);
		Map<String, Object> map = new HashMap<>();
		map.put("result", true);
		return map;
	}
	
	@InitBinder
	public void initBind(WebDataBinder binder) throws Exception {
		CustomDateEditor customDateEditor = new CustomDateEditor(new SimpleDateFormat("yyyy-MM-dd"), true);
		binder.registerCustomEditor(Date.class, customDateEditor);

	}

}
